<?php
class ShopComponent extends Component {
	public $components = array('Session');
	public $maxQuantity = 100;
	public function shop() {
		$shop = $this->Session->read('Shop');
		$quantity = 0;
		$weight = 0;
		$subtotal = 0;
		$total = 0;
		$order_item_count = 0;

		if (count($shop['OrderItem']) > 0) {
			foreach ($shop['OrderItem'] as $item) {
				$quantity += $item['quantity'];
				$total += $item['subtotal'];
				$order_item_count++;
			}
			$d['order_item_count'] = $order_item_count;
			$d['quantity'] = $quantity;
			$d['total'] = $total;
			$this->Session->write('Shop.Order', $d);
			return true;
		}
		else {
			$d['quantity'] = 0;
			$d['weight'] = 0;
			$d['subtotal'] = 0;
			$d['total'] = 0;
			$this->Session->write('Shop.Order', $d);
			return false;
		}
	}
	public function add($id, $product, $quantity){
			$item = $product;
			$item["quantity"] = $quantity;
			$item['subtotal'] = sprintf($product['Product']['price'] * $quantity);
			if(!empty($product)) {
				// if($quantity == 0){
					// $this->Session->delete('Shop.OrderItem.' . $id);
				// }else{
					// $this->Session->write('Shop.OrderItem.' . $id , $item);
				// }
				$this->Session->write('Shop.OrderItem.' . $id , $item);
			}
			$this->shop();
	}
}